package com.epfresh.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.bean.HomeColumnBean;
import com.epfresh.bean.NewHomeBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Chen
 * Date: 2019/4/22
 * Desc:
 */
public class ColumnProductsAdapter extends RecyclerView.Adapter<ColumnProductsAdapter.ViewHolder> {
    private List<HomeColumnBean.ProductsBean> productsBeans = new ArrayList<>();
    private int productsCount;
    private OnColumnProductsClickListener onColumnProductsClickListener;

    public void setOnColumnProductsClickListener(OnColumnProductsClickListener onColumnProductsClickListener) {
        this.onColumnProductsClickListener = onColumnProductsClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_limited_time_special_good, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        if (productsBeans != null && productsBeans.size() > 0) {

            if (!TextUtils.isEmpty(productsBeans.get(position).getThumbnail())) {
                ImageLoader.getInstance().displayImage(productsBeans.get(position).getThumbnail(), holder.iv_product);
            }
            if (productsBeans.get(position).getName() != null && !productsBeans.get(position).getName().equals("默认")) {
                holder.txt_package.setText(productsBeans.get(position).getName() + "(" + productsBeans.get(position).getPack() + ")");
            } else {
                holder.txt_package.setText(productsBeans.get(position).getPack());
            }
            holder.txt_product_title.setText(productsBeans.get(position).getProductParseDto().getTitle());
            holder.txt_price.setText("¥" + productsBeans.get(position).getProductSpecPrice().getPrice() + "/" + productsBeans.get(position).getChargeUnit());

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onColumnProductsClickListener != null) {
                        onColumnProductsClickListener.onColumnProductClick(productsBeans.get(holder.getAdapterPosition()));
                    }
                }
            });
            holder.rl_center_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onColumnProductsClickListener != null) {
                        onColumnProductsClickListener.showMore();
                    }
                }
            });
            if (position == productsBeans.size() - 1 && productsCount > 8) {
                holder.rl_center_more.setVisibility(View.VISIBLE);
                holder.tv_center_number.setText("共" + productsCount + "件");
            } else {
                holder.rl_center_more.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public int getItemCount() {
        return productsBeans == null ? 0 : productsBeans.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_product, iv_tag;
        private TextView txt_product_title, txt_package, txt_price, tv_center_number, tv_finish_flag;
        private View rl_center_more;

        ViewHolder(View itemView) {
            super(itemView);

            txt_product_title = itemView.findViewById(R.id.txt_product_title);
            iv_product = itemView.findViewById(R.id.iv_product);
            iv_tag = itemView.findViewById(R.id.iv_tag);
            txt_package = itemView.findViewById(R.id.txt_package);
            txt_price = itemView.findViewById(R.id.txt_price);
            rl_center_more = itemView.findViewById(R.id.rl_center_more);
            tv_center_number = itemView.findViewById(R.id.tv_center_number);
            tv_finish_flag = itemView.findViewById(R.id.tv_finish_flag);
        }
    }

    public void setProducts(List<HomeColumnBean.ProductsBean> productsBeans, int productsCount) {
        this.productsCount = productsCount;
        this.productsBeans.clear();
        this.productsBeans.addAll(productsBeans);
        notifyDataSetChanged();
    }

    public interface OnColumnProductsClickListener {
        void onColumnProductClick(HomeColumnBean.ProductsBean productsBean);

        void showMore();
    }

}
